/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import cn.webrx.mapper.StudentMapper;
import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.*;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * <p>Project: mybatis2021 - App
 * <p>Powered by webrx On 2021-07-14 09:38:53
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Slf4j
public class App {
    protected SqlSession ss;



    public static void main(String[] args) throws IOException {
        //建立数据源，
        DruidDataSource ds = new DruidDataSource();
        Properties prop = new Properties();
        prop.load(Resources.getResourceAsReader("db.properties"));
        ds.setConnectProperties(prop);

        TransactionFactory tx = new JdbcTransactionFactory();
        Environment env = new Environment("development", tx, ds);
        Configuration cfg = new Configuration(env);

        cfg.addMappers("cn.webrx.mapper");
        //添加拦截器插件
        PageInterceptor pi = new PageInterceptor();
        cfg.addInterceptor(pi);

        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
        SqlSession ss = sf.openSession();

        StudentMapper sm = ss.getMapper(StudentMapper.class);

        //分页
        //PageHelper.startPage(1, 3);
        //分页+排序功能
        //PageHelper.startPage(1, 3, "id desc");
        //分页+1,3) 默认是true 计算记录总数，如果是false,不计算总记录数 没有select count(*) , pp.getTotal() = -1
        //PageHelper.startPage(1,3,false); //没有select count(*)
        //PageHelper.startPage(1,3,true); //有select count(*)  pp.getTotal()=19

        //第一个 true 是否查询 select count(*) 求
        //第二个 true 分页的合理化，如果pageNum<=0 显示第1页 pageNum>pages 显示最pages页 最后一页
        //第三个 true 代码如果pagesize = 0,查看所有，就是不分页，

        //PageHelper.startPage(1, 5, true, true, true).setOrderBy("money desc");
        PageHelper.startPage(1, 10, true, true, true).setOrderBy("name desc,money desc,id desc");

        //PageInfo pp = PageInfo.of(sm.query("name = '李丽' or name ='李燕'"));
        //PageInfo pp = PageInfo.of(sm.query("name like '李%'"));
        //PageInfo pp = PageInfo.of(sm.query("money >= 100"));
        PageInfo pp = PageInfo.of(sm.query());

        System.out.println(pp.getPageNum());//当前页 2
        System.out.println(pp.getPages());//共有多少页 7
        System.out.println(pp.getPageSize());//每页多少条 3
        System.out.println(pp.getTotal());//总记录数 总条数  19
        List<Map<String, Object>> list = pp.getList();//获取当前页的数据
        list.forEach(System.out::println);

    }


    public static void main3(String[] args) throws IOException {
        //建立数据源，
        DruidDataSource ds = new DruidDataSource();
        Properties prop = new Properties();
        prop.load(Resources.getResourceAsReader("db.properties"));
        ds.setConnectProperties(prop);

        TransactionFactory tx = new JdbcTransactionFactory();
        Environment env = new Environment("development", tx, ds);
        Configuration cfg = new Configuration(env);

        cfg.addMappers("cn.webrx.mapper");

        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
        SqlSession ss = sf.openSession();

        StudentMapper sm = ss.getMapper(StudentMapper.class);

        int currpage = 4;
        int pagesize = 3;

        RowBounds rows = new RowBounds(currpage * pagesize - pagesize, pagesize);
        System.out.println(sm.page2(null, rows));
    }

    public static void main2(String[] args) throws IOException {
        //建立数据源，
        DruidDataSource ds = new DruidDataSource();
        Properties prop = new Properties();
        prop.load(Resources.getResourceAsReader("db.properties"));
        ds.setConnectProperties(prop);

        TransactionFactory tx = new JdbcTransactionFactory();
        Environment env = new Environment("development", tx, ds);
        Configuration cfg = new Configuration(env);


        cfg.addMappers("cn.webrx.mapper");


        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);


        SqlSession ss = sf.openSession();

        StudentMapper sm = ss.getMapper(StudentMapper.class);
        //System.out.println(sm.query());

        //统计记录总数
        int recordcount = sm.count();
        //当前要显示的页数第几页
        int currpage = 3;
        //每页多少条
        int pagesize = 3;
        int start = currpage * pagesize - pagesize;
        //总页数
        int pagecount = recordcount % pagesize == 0 ? recordcount / pagesize : recordcount / pagesize + 1;

        System.out.printf("【第%d页，共%d页】【每页%d条，共%d条】%n", currpage, pagecount, pagesize, recordcount);

        System.out.println(sm.page(start, pagesize));

        ss.commit();


    }
}


